using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Decav.Gatsb.Web;
using Decav.Gatsb.Application;
using System.Net.Mail;
using System.IO;
using Decav.Gatsb.Diagnostics;

public partial class Contact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        if (!CustomValidate())
            return;

        MailMessage msg = new MailMessage();
        msg.Subject = string.Format("{0}: {1}", aboutDropDown.SelectedValue, subjectTextBox.Text);
        msg.To.Add("me@decav.com");
        msg.IsBodyHtml = false;
        msg.Body = string.Format("From: {0}\r\n\r\n{1}",
            GatsbApplication.Current.UserPresent ? GatsbApplication.Current.CurrentUser.Id.ToString() : "(Unknown)",
            bodyTextBox.Text);

        GatsbWebApplication.Current.SendMail(msg);

        try
        {
            File.WriteAllText(
                string.Format(@"c:\gatsb\contact\{0}.txt", Guid.NewGuid().ToString()),
                string.Format("{0}\r\n{1}", msg.Subject, msg.Body));
        }
        catch (Exception ex)
        {
            GatsbDiagnostics.WriteException("Error sending message", ex);
        }


        submitForm.Visible = false;
        completedDiv.Visible = true;
    }

    private bool CustomValidate()
    {
        if (aboutDropDown.SelectedValue == null || aboutDropDown.SelectedValue.Length == 0)
        {
            SetError("Please tell us what this email is about.");
            return false;
        }

        if (bodyTextBox.Text.Trim().Length == 0)
        {
            SetError("There is no message in this email you're sending us!");
            return false;
        }

        return true;
    }

    private void SetError(string err)
    {
        formErrorDiv.Visible = true;
        formErrorDiv.InnerText = err;
    }
}
